#!/bin/bash
#
# Copyright (C) 2009-2014:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    David GUENAULT, dguenault@monitoring-fr.org
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

# temporary path
export TMP=${TMP:-"/tmp"}
export LOGFILE=${LOGFILE:-"$TMP/shinken.install.log"}
# proxy support
export USEPROXY=${USEPROXY:-0}
# Skip prerequisites installation
export SKIPPREREQUISITES=${SKIPPREREQUISITES:-0}
# for shinken
export VERSION=${VERSION:-"master"}
export TARGET=${TARGET:-"/var/lib/shinken"}
export ETC=${ETC:-"/etc/shinken"}
export LIBEXEC=${LIBEXEC:-"$TARGET/libexec"}
export VAR=${VAR:-"/var/log/shinken"}
export BACKUPDIR=${BACKUPDIR:-"/opt/backup"}
export SKUSER=${SKUSER:-"shinken"}
export SKGROUP=${SKGROUP:-"shinken"}
export MANAGEPYRO=${MANAGEPYRO:-0}
export KEEPDAYSLOG=7
# default retention module (pickle|mongodb)
# work only if ENABLEOPTPKGS
export RETENTIONMODULE=${RETENTIONMODULE:-"pickle"}
export LOGSTORE=${LOGSTORE:-"sqlite"}
export MONGOSERVER=${MONGOSERVER:-"localhost"}
# Quiet mode (default answer for all questions)
export QUIET=${QUIET:-0}
# FOR CES INTEGRATION
export IF="eth0" # local iface
export ADDPOLLERS="poller-2=172.16.1.2"
# DO NOT MODIFY THIS
export DISTROS="REDHAT:5 REDHAT:6 DEBIAN Fedora:16 Fedora:17 Fedora:18"
export DATE=$(date +%Y%m%d%H%M%S)
export GIT="https://github.com/naparuba/shinken.git"

export ARCH=$(uname -i)
if [ "$ARCH" = "unknown" ]
then
    ARCH=$(uname -m)
fi

LSB_RELEASE="/usr/bin/lsb_release"

if test -x "${LSB_RELEASE}"; then

    export DIST="`${LSB_RELEASE} -i | awk '{print $NF}'`"
    export VERS="`${LSB_RELEASE} -r | awk '{print $NF}' | awk -F. '{print $1}'`"

else

    echo "You must install lsb_release binary before launching this install script."
    exit 2

fi

case $DIST in
    CentOS)
        export CODE="REDHAT"
                export WEBSERVER="httpd"
                HTTPDUSER="apache"
                HTTPDGROUP="apache"
        ;;
    EnterpriseEnterpriseServer)
        export CODE="REDHAT"
                export WEBSERVER="httpd"
                HTTPDUSER="apache"
                HTTPDGROUP="apache"
        ;;
    OracleServer)
        export CODE="REDHAT"
                export WEBSERVER="httpd"
                HTTPDUSER="apache"
                HTTPDGROUP="apache"
        ;;
    Scientific)
        export CODE="REDHAT"
                export WEBSERVER="httpd"
                HTTPDUSER="apache"
                HTTPDGROUP="apache"
        ;;
    RedHatEnterpriseServer)
        export CODE="REDHAT"
                export WEBSERVER="httpd"
                HTTPDUSER="apache"
                HTTPDGROUP="apache"
        ;;
    Fedora)
        export CODE="Fedora"
                export WEBSERVER="httpd"
                HTTPDUSER="apache"
                HTTPDGROUP="apache"
        ;;
    Debian)
        export CODE="DEBIAN"
                export WEBSERVER="apache2"
                HTTPDUSER="www-data"
                HTTPDGROUP="www-data"
        ;;
    Ubuntu)
        export CODE="DEBIAN"
                export WEBSERVER="apache2"
                HTTPDUSER="www-data"
                HTTPDGROUP="www-data"
        ;;
    LinuxMint)
        export CODE="DEBIAN"
                export WEBSERVER="apache2"
                HTTPDUSER="www-data"
                HTTPDGROUP="www-data"
        ;;
    *)
        export CODE=""
        ;;
esac

which systemctl > /dev/null 2>&1
if [ $? -eq 0 ]
then
    export INIT="systemd"
else
    export INIT="init"
fi

case $CODE in
    REDHAT)
    	if [ "$VERS" = "5" ]
        then
            export EPELVERS="5-4"
            #export YUMPKGS="gcc python26 sqlite-devel python26-devel git mysql-devel nmap wget unzip pymongo-gridfs pymongo"
            export YUMPKGS="gcc python26 sqlite-devel python26-devel git mysql-devel nmap unzip python26-ldap net-snmp python26-pycurl"
            export PYLIBSRHEL="paramiko:paramiko netifaces:netifaces multiprocessing:multiprocessing simplejson:simplejson pysqlite:sqlite3 MySQL-python:MySQLdb Pyro4:Pyro4 pymongo:pymongo kombu:kombu paste:paste"
        elif [ "$VERS" = "6" ]
            then
            export EPELVERS="6-8"
            #export YUMPKGS="gcc python sqlite-devel python-devel git mysql-devel python-setuptools nmap wget unzip pymongo-gridfs pymongo"
            #export YUMPKGS="gcc python sqlite-devel python-devel git mysql-devel python-setuptools nmap unzip python-ldap python-paste python-pycurl net-snmp-python net-snmp mysql-libs"
            export YUMPKGS="gcc python sqlite-devel python-devel git mysql-devel nmap unzip python-ldap python-paste python-pycurl net-snmp-python net-snmp mysql-libs"
            export PYLIBSRHEL6="paramiko:paramiko pymongo:pymongo netifaces:netifaces simplejson:simplejson pysqlite:sqlite3 MySQL-python:MySQLdb Pyro4:Pyro4 kombu:kombu"
        fi
        export EPELNAME="epel-release"
        export EPELPKG="${EPELNAME}-${EPELVERS}.noarch.rpm"
        export EPEL="http://download.fedoraproject.org/pub/epel/$VERS/$ARCH/$EPELPKG"
        export MONGOPKGS="mongo-10gen mongo-10gen-server "
        export THRUKPKGS="thruk"
        ;;
    Fedora)
        # Don't forget trailing / for SYSTEMDPATH. It will doesn't work without.
        if [ "$VERS" = "16" ]
        then
            #export YUMPKGS="gcc python26 sqlite-devel python26-devel git mysql-devel nmap unzip python-ldap python-setuptools python-paramiko python-simplejson python-paste python-pycurl net-snmp-python"
            export YUMPKGS="gcc python26 sqlite-devel python26-devel git mysql-devel nmap unzip python-ldap python-paramiko python-simplejson python-paste python-pycurl net-snmp-python"
            export PYLIBS="netifaces:netifaces multiprocessing:multiprocessing pysqlite:sqlite3 MySQL-python:MySQLdb Pyro4:Pyro4 kombu:kombu pymongo:pymongo"
            export SYSTEMDPATH="/"
        elif [ "$VERS" = "17" -o "$VERS" = "18" ]
        then
            #export YUMPKGS="gcc python sqlite-devel python-devel git mysql-devel nmap unzip python-ldap python-setuptools python-paramiko python-simplejson python-pyro python-kombu python-sqlite3dbm python-netifaces MySQL-python python-paste python-pycurl net-snmp-python"
            export YUMPKGS="gcc python sqlite-devel python-devel git mysql-devel nmap unzip python-ldap python-paramiko python-simplejson python-pyro python-kombu python-sqlite3dbm python-netifaces MySQL-python python-paste python-pycurl net-snmp-python"
            export PYLIBS="multiprocessing:multiprocessing"
            export SYSTEMDPATH="/usr/"
        fi
            export MONGOPKGS="mongodb-server pymongo"
            export SYSTEMDSERVICES="shinken-arbiter.service shinken-broker.service shinken-poller.service shinken-reactionner.service shinken-receiver.service shinken-scheduler.service"
        ;;
    DEBIAN)
        if [ "$VERS" = "6" ]
        then
            # workaround a problem with pyro3 on debian squeeze
            export PYLIBSDEB="paramiko:paramiko netifaces:netifaces simplejson:simplejson pysqlite:sqlite3 MySQL_python:MySQLdb pymongo:pymongo Pyro4:Pyro4 kombu:kombu"
            #export APTPKGS="build-essential libperl-dev python-setuptools libsqlite3-dev python-dev sqlite3 nmap unzip libmysqlclient-dev python-ldap libevent-dev python-paste python-pycurl libsnmp-python snmpd"
            export APTPKGS="build-essential libperl-dev libsqlite3-dev python-dev sqlite3 nmap unzip libmysqlclient-dev python-ldap libevent-dev python-paste python-pycurl libsnmp-python snmpd"
            export MONGOPKGS="mongodb-10gen"
            export THRUKPKGS="thruk"
        else
            export PYLIBSDEB="paramiko:paramiko netifaces:netifaces simplejson:simplejson pysqlite:sqlite3 MySQL_python:MySQLdb pymongo:pymongo kombu:kombu"
            #export APTPKGS="build-essential libperl-dev python-setuptools libsqlite3-dev python-dev pyro sqlite3 nmap unzip libmysqlclient-dev python-ldap libevent-dev python-paste python-pycurl libsnmp-python snmpd"
            export APTPKGS="build-essential libperl-dev libsqlite3-dev python-dev pyro sqlite3 nmap unzip libmysqlclient-dev python-ldap libevent-dev python-paste python-pycurl libsnmp-python snmpd"
            export MONGOPKGS="mongodb-10gen"
        fi
        ;;
esac

export SETUPTOOLSVERS="1.1.6"
export SETUPTOOLS="https://pypi.python.org/packages/source/s/setuptools/setuptools-$SETUPTOOLSVERS.tar.gz"

#export VSPHERESDKAPTPKGS="libssl-dev perl-doc liburi-perl libxml-libxml-perl libcrypt-ssleay-perl ia32-libs libclass-methodmaker-perl libsoap-lite-perl libsoap-lite-perl libuuid-perl make libnagios-plugin-perl"
export VSPHERESDKVER="5.1.0-780721"
export VSPHERESDK="http://www.shinken-monitoring.org/archives/VMware-vSphere-SDK-for-Perl-$VSPHERESDKVER.$ARCH.tar.gz"
export CHECK_ESX3_SCRIPT="http://www.shinken-monitoring.org/archives/check_esx3.pl"
export CHECK_DISK_VCENTER_SCRIPT="http://www.shinken-monitoring.org/archives/check_disk_vcenter.pl"
if [ "$ARCH" = "x86_64" ]
then
    export VSPHERESDKAPTPKGS="libssl-dev perl-doc liburi-perl libxml-libxml-perl libcrypt-ssleay-perl ia32-libs libclass-methodmaker-perl libsoap-lite-perl make libnagios-plugin-perl"
else
    export VSPHERESDKAPTPKGS="libssl-dev perl-doc liburi-perl libxml-libxml-perl libcrypt-ssleay-perl libclass-methodmaker-perl libsoap-lite-perl make libnagios-plugin-perl"
fi
export VSPHERESDKYUMPKGS="openssl-devel perl-URI perl-XML-LibXML perl-Crypt-SSLeay perl-SOAP-Lite perl-Class-MethodMaker uuid-perl make perl-libwww-perl.noarch"

export NAGPLUGYUMPKGS="perl-Class-Accessor perl-Config-Tiny perl-Math-Calc-Units perl-Params-Validate"
export NAGPLUGPERL="http://search.cpan.org/CPAN/authors/id/T/TO/TONVOON/Nagios-Plugin-0.36.tar.gz"
export NAGPLUGVERS="1.4.15"
export NAGPLUGBASEURI="https://www.nagios-plugins.org/download/nagios-plugins-$NAGPLUGVERS.tar.gz"
export NAGPLUGAPTPKG="libgnutls-dev libmysqlclient-dev libssl-dev libsnmp-perl libkrb5-dev libldap2-dev libsnmp-dev libnet-snmp-perl gawk libwrap0-dev libmcrypt-dev fping snmp gettext smbclient dnsutils"
export NAGPLUGYUMPKG="gnutls-devel mysql-devel openssl-devel net-snmp-perl krb5-devel openldap-devel net-snmp-devel gawk libmcrypt-devel fping net-snmp gettext samba-client"

export CHECKNETAPP2="http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=389\&cf_id=24"
export CHECKNETAPP2APTPKGS="snmp libnet-snmp-perl"
export CHECKNETAPP2YUMPKGS="perl-Net-SNMP net-snmp"

export CAPTURE_PLUGIN="http://www.waggy.at/nagios/capture_plugin.txt"

export CHECKORACLEHEALTHAPTPKG="libdbi-perl libaio1"
export CHECKORACLEHEALTHYUMPKG="perl-DBI libaio"
export CHECKORACLEHEALTHCPAN="http://search.cpan.org/CPAN/authors/id/P/PY/PYTHIAN/DBD-Oracle-1.36.tar.gz"
export CHECKORACLEHEALTH="http://labs.consol.de/wp-content/uploads/2011/09/check_oracle_health-1.7.3.tar.gz"

export CHECKLOGFILES="http://labs.consol.de/download/shinken-nagios-plugins/check_logfiles-3.5.3.2.tar.gz"

export CHECKDB2HEALTHAPTPKG="libdbi-perl libaio1"
export CHECKDB2HEALTHYUMPKG="perl-DBI libaio"
export CHECKDB2HEALTHCPAN="http://search.cpan.org/CPAN/authors/id/I/IB/IBMTORDB2/DBD-DB2-1.85.tar.gz"
export CHECKDB2HEALTH="http://labs.consol.de/wp-content/uploads/2010/12/check_db2_health-1.0.3.tar.gz"

export CHECKMONGOAPTPKG="" #python-pymongo"
export CHECKMONGOYUMPKG="" #pymongo"
export CHECKMONGO="https://raw.github.com/mzupan/nagios-plugin-mongodb/master/check_mongodb.py"

export CHECKIBMDSHEALTH="http://www.shinken-monitoring.org/archives/check_IBM_DS_health.sh"
export CHECKIBMDSPERF="http://www.shinken-monitoring.org/archives/check_IBM_performance_v1.4"
export CHECKIBM="http://www.shinken-monitoring.org/pub/plugins/check_snmp_systemHealth.pl http://www.shinken-monitoring.org/pub/plugins/check_snmp_HACMP.pl http://www.shinken-monitoring.org/pub/plugins/check_snmp_Safekit.pl http://www.shinken-monitoring.org/pub/plugins/check_snmp_aixVGstate.pl"
export CHECKHPUX="http://www.shinken-monitoring.org/pub/plugins/check_snmp_hpux_storage.pl http://www.shinken-monitoring.org/pub/plugins/check_snmp_hpux_mem"

export CHECKVMWARESNAPSHOTS="http://labs.consol.de/wp-content/uploads/2012/10/check_vmware_snapshots_0.13.tar.gz"

export CHECKEMCAPTPKG=""
export CHECKEMCYUMPKG=""
export CHECKEMC="https://www.netways.org/attachments/download/393/check_emc.zip"

export CHECKMEM="https://github.com/justintime/nagios-plugins/tarball/master"

export CHECKNWCAPTPKG="autoconf automake m4 libtool"
export CHECKNWCYUMPKG="autoconf automake m4 libtool"
export CHECKNWC="https://github.com/lausser/check_nwc_health/tarball/1.3"

export CHECKMYSQLHEALTH="http://labs.consol.de/wp-content/uploads/2011/08/check_mysql_health-2.1.7.tar.gz"
export CHECKMYSQLHEALTHYUMPKG="perl-Time-HiRes"
export CHECKMYSQLHEALTHAPTPKG="libclass-dbi-mysql-perl"

export PNPBRANCH="0.6"
export PNPRELEASE="19"
export PNPPREFIX="/usr/local/pnp4nagios"
export PNPURI="http://downloads.sourceforge.net/project/pnp4nagios/PNP-$PNPBRANCH/pnp4nagios-$PNPBRANCH.$PNPRELEASE.tar.gz"
export PNPAPTPKG="rrdtool apache2 libapache2-mod-php5 librrds-perl php5-gd"
export PNPYUMPKG="rrdtool httpd php rrdtool-perl php-gd perl-Time-HiRes"

export CARBON_GITHUB="https://github.com/graphite-project/carbon.git -b 0.9.x"
export GRAPHITE_WEB_GITHUB="https://github.com/graphite-project/graphite-web.git -b 0.9.x"
export WHISPER_GITHUB="https://github.com/graphite-project/whisper.git -b 0.9.x"
export GRAPHITEPKG="txamqp twisted django django-tagging"
export GRAPHITEPKGDEB="libapache2-mod-wsgi python-cairo"

export NAGVIS="http://downloads.sourceforge.net/project/nagvis/NagVis%201.6/nagvis-1.6.3.tar.gz"
export NAGVISPREFIX="/usr/local/nagvis"
export NAGVISAPTPKGS="apache2 libapache2-mod-php5 php5-gd php5-sqlite php-net-socket php5-mysql graphviz"
export NAGVISYUMPKGS="httpd php php-gd php-sqlite php-mysql graphviz"

export MKVER="1.2.2p2"
export MKURI="http://mathias-kettner.de/download/check_mk-$MKVER.tar.gz"
export MKPREFIX="/usr/local/check_mk"
export MKAPTPKG="libapache2-mod-python sudo"
export MKYUMPKG="mod_python sudo"

export WMICAPTPKG="autoconf libdatetime-perl libnagios-plugin-perl libconfig-inifiles-perl libnumber-format-perl"
export WMICYUMPKG="autoconf python perl-DateTime perl-Config-IniFiles perl-Number-Format"
export WMIC="http://www.shinken-monitoring.org/archives/wmi-1.3.14.tar.gz"
export CHECKWMIPLUS="http://edcint.co.nz/checkwmiplus/sites/default/files/check_wmi_plus.v1.55.tar.gz"

export CHECKHPASM="http://labs.consol.de/download/shinken-nagios-plugins/check_hpasm-4.6.3.2.tar.gz"
export CHECKHPASMAPTPKGS="snmp libnet-snmp-perl"
export CHECKHPASMYUMPKGS="perl-Net-SNMP net-snmp"

export MANUBULON="http://nagios.manubulon.com/nagios-snmp-plugins.1.1.1.tgz"
export MANUBULONYUMPKG="perl-Net-SNMP"
export MANUBULONAPTPKG="libsnmp-perl"

export CHECKSNMPBANDWIDTH="https://gitorious.org/check_snmp_bandwidth/check_snmp_bandwidth/blobs/raw/stable/check_snmp_bandwidth"
export CHECKSNMPBANDWIDTHPNP="https://gitorious.org/check_snmp_bandwidth/check_snmp_bandwidth/blobs/raw/stable/pnp4nagios/check_snmp_bandwidth.php"

export CHECKNETINT="http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=1475&cf_id=35"

export NCONF="http://downloads.sourceforge.net/project/nconf/nconf/1.3.0-0/nconf-1.3.0-0.tgz"
export NCONFYUMPKG="httpd php php-ldap php-mysql perl-DBI perl-DBD-MySQL"
export NCONFAPTPKG="debconf-utils apache2 php5 php5-ldap php5-mysql libdbi-perl libdbd-mysql-perl"

export CHECKRSYNC="http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=307&cf_id=29"

# experimental for application performance monitoring
export EUEYUMPKG="ruby rubygems ruby-devel libxml2-devel libxslt-devel openssl-devel gcc gcc-c++ automake autoconf libtool make firefox xorg-x11-server-Xvfb"
export EUEGEMS="bson_ext:1.6.4 mongo:1.6.4 unicode:0.4.3 inifile:1.1.0 cucumber:1.2.1 rspec:2.11.0 watir-webdriver:0.6.1 headless:0.3.1"


###################################
### TEXT PRESENTATION FUNCTIONS ###
###################################

function cecho ()
{

        # Argument $1 = message
        # Argument $2 = foreground color
        # Argument $3 = background color
        # Argument $4 = if not zero then do not add line feed

        # if term is not set simply write the output
        if [ -n $TERM ]
        then
                echo -ne $1"\n" >> $LOGFILE
                echo -ne $1"\n"
                return
        fi

        case "$2" in
                "black")
                        fcolor='30'
                        ;;
                "red")
                        fcolor='31'
                        ;;
                "green")
                        fcolor='32'
                        ;;
                "yellow")
                        fcolor='33'
                        ;;
                "blue")
                        fcolor='34'
                        ;;
                "magenta")
                        fcolor='35'
                        ;;
                "cyan")
                        fcolor='36'
                        ;;
                "white")
                        fcolor='37'
                        ;;
                *)
                        fcolor=''
        esac
        case "$3" in
                "black")
                        bcolor='40'
                        ;;
               "red")
                        bcolor='41'
                        ;;
                "green")
                        bcolor='42'
                        ;;
                "yellow")
                        bcolor='43'
                        ;;
                "blue")
                        bcolor='44'
                        ;;
                "magenta")
                        bcolor='45'
                        ;;
                "cyan")
                        bcolor='46'
                        ;;
                "white")
                        bcolor='47'
                        ;;
                *)
                        bcolor=""
        esac

        if [ -z $bcolor ]
        then
                echo -ne $1"\n" >> $LOGFILE
                echo -ne "\E["$fcolor"m"$1"\n"
        else
                echo -ne $1"\n" >> $LOGFILE
                echo -ne "\E["$fcolor";"$bcolor"m"$1"\n"
        fi
        tput sgr0
        return
}

function cread(){
    # $1: text
    # $2: color
    # $3: default
    # $4: choices

        # Argument $4 = if not zero then do not add line feed
        case "$2" in
                "black")
                        fcolor='30'
                        ;;
                "red")
                        fcolor='31'
                        ;;
                "green")
                        fcolor='32'
                        ;;
                "yellow")
                        fcolor='33'
                        ;;
                "blue")
                        fcolor='34'
                        ;;
                "magenta")
                        fcolor='35'
                        ;;
                "cyan")
                        fcolor='36'
                        ;;
                "white")
                        fcolor='37'
                        ;;
                *)
                        fcolor=''
        esac


        if [ $QUIET -eq 1 ]
        then
            readvalue=$3
            return
        fi

        if [ -z $bcolor ]
        then
                echo -ne "\E["$fcolor"m"$1""
        else
                echo -ne "\E["$fcolor";"$bcolor"m"$1""
        fi

        read readvalue

        if [ -z "$readvalue" ]
        then
            readvalue=$3
            tput sgr0
            return
        else
            if [ "$4" = "nocheck" ]
            then
                return
            fi
            match=0
            for av in $4
            do
                if [ "$av" = "$readvalue" ]
                then
                    match=1
                fi
            done
            if [ $match -eq 0 ]
            then
                tput sgr0
                cecho " > Invalid value ($readvalue) allowed values are [$4]" red
                cread "$1" "$2" "$3" "$4"
                return
            else
                tput sgr0
                return
            fi
        fi
}

function creads(){
    # $1: text
    # $2: color

        # Argument $4 = if not zero then do not add line feed
        case "$2" in
                "black")
                        fcolor='30'
                        ;;
                "red")
                        fcolor='31'
                        ;;
                "green")
                        fcolor='32'
                        ;;
                "yellow")
                        fcolor='33'
                        ;;
                "blue")
                        fcolor='34'
                        ;;
                "magenta")
                        fcolor='35'
                        ;;
                "cyan")
                        fcolor='36'
                        ;;
                "white")
                        fcolor='37'
                        ;;
                *)
                        fcolor=''
        esac


        if [ -z $bcolor ]
        then
                echo -ne "\E["$fcolor"m"$1" "
        else
                echo -ne "\E["$fcolor";"$bcolor"m"$1" "
        fi

        read readvalue

       tput sgr0
       return
}

function cadre(){
    # display a colored enclosure
    # $1 => text
    # $2 => color
    cecho "+--------------------------------------------------------------------------------" $2
    cecho "| $1" $2
    cecho "+--------------------------------------------------------------------------------" $2
}

######################
### MISC FUNCTIONS ###
######################

function pyroset(){
    case $CODE in
        REDHAT)
            if [ "$VERS" = "6" ]
            then
                export PYRO="Pyro4==4.12"
            fi
            ;;
        Fedora)
            export PYRO="Pyro4==4.12"
            ;;
        DEBIAN)
            if [ "$VERS" = "6" ]
            then
                export PYRO="Pyro4==4.12"
            elif [ "$VERS" = "5" ]
            then
                export PYRO="Pyro==3.16"
            else
                export PYRO="Pyro4==4.12"
            fi
            ;;
    esac
}

#Check if we launch the script with root privileges (aka sudo)
if [ "$UID" != "0" ]
then
        echo "You should start the script with sudo!"
        exit 2
fi

function pythonver(){
    versions="2.4 2.5 2.6 2.7"
        LASTFOUND=""
        # is there any python here?
        for v in $versions
        do
                which python$v >> /tmp/shinken.install.log 2>&1
                if [ $? -eq 0 ]
                then
                        LASTFOUND="python$v"
                fi
        done
        if [ -z "$LASTFOUND" ]
        then
                # finaly try to find a default python
                which python >> /tmp/shinken.install.log 2>&1
                if [ $? -ne 0 ]
                then
                        echo "No python interpreter found!"
                        exit 2
                else
                        echo "python found"
                        LASTFOUND=$(which python)
                fi
        fi
        PY=$LASTFOUND
    echo $PY
}
function pythonvershort(){
    versions="2.4 2.5 2.6 2.7"
        LASTFOUND=""
        # is there any python here?
        for v in $versions
        do
                which python$v >> /tmp/shinken.install.log 2>&1
                if [ $? -eq 0 ]
                then
                        LASTFOUND="$v"
                fi
        done
        PY=$LASTFOUND
    echo $PY
}

function rheldvd(){
    # this is only for my personal needs
    # dvd mounted?
    if [ "$CODE" != "REDHAT" ]
    then
        cecho " > Only for REDHAT" red
        exit 2
    fi
    cadre "Setup rhel dvd for yum (this is only for my development purpose)" green
    dvdm=$(cat /proc/mounts | grep "^\/dev\/cdrom")
    if [ -z "$dvdm" ]
    then
        if [ ! -d "/media/cdrom" ]
        then
            mkdir -p "/media/cdrom"
        fi
        cecho " > Insert RHEL/CENTOS DVD and press ENTER" yellow
        read -p " > ENTER when ready "
        mount -t iso9660 -o ro /dev/cdrom /media/cdrom >> /tmp/shinken.install.log 2>&1
        if [ $? -eq 0 ]
        then
            dvdm=$(cat /proc/mounts | grep "^\/dev\/cdrom")
            if [ -z "$dvdm" ]
            then
                cecho " > Unable to mount RHEL/CENTOS DVD!" red
                exit 2
            else
                if [ ! -d "/media/cdrom/Server" ]
                then
                    cecho "Invalid DVD" red
                    exit 2
                else
                    if [ ! -f "/etc/yum.repos.d/rheldvd.repo" ]
                    then
                        echo "[dvd]" > /etc/yum.repos.d/rheldvd.repo
                        echo "name=rhel dvd" >> /etc/yum.repos.d/rheldvd.repo
                        echo "baseurl=file:///media/cdrom/Server" >> /etc/yum.repos.d/rheldvd.repo
                        echo "enabled=1" >> /etc/yum.repos.d/rheldvd.repo
                        echo "gpgcheck=0" >> /etc/yum.repos.d/rheldvd.repo
                    fi
                fi
            fi
        else
            cecho " > Error while mounting DVD" red
        fi
    fi
}

if [ $USEPROXY -eq 1 ]
then
    cecho "WARNNG: USE PROXY IS ENABLED" yellow
    cecho "YOU MUST VERIFY THAT http_proxy and https_proxy IS ENABLED" yellow
    cecho "YOU MUST VERIFY THAT YOUR PACKAGE MANAGER IS CONFIGURED TO USE PROXY" yellow
    cecho "HIT ENTER TO CONTINUE OR CTRL+C TO ABORT" yellow
    read
fi
